package com.hankol.winecellar

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseActivity
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.hankol.winecellar.adapter.CommonManageAdapter
import com.hankol.winecellar.bean.WineryEvaluateListBean
import com.hankol.winecellar.databinding.ActivityCommentManageBinding
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.winecellar.viewmodel.WineCellarViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**酒窖评论管理
 * @author: merbng
 * @description:
 */
@Route(path = ActivityRouter.WINE_CELLAR.WINCE_COMMENT_MANAGER)
class CommentManageActivity : BaseActivity(), View.OnClickListener {

    private lateinit var viewModel: WineCellarViewModel
    private lateinit var binding: ActivityCommentManageBinding
    val mAdapter: CommonManageAdapter by lazy { CommonManageAdapter() }
    private var hasnext: Boolean = false
    var page = 1
    //当前点击的item的position
    var clickPosition = 0
    override fun getLayoutId(): Int {
        return R.layout.activity_comment_manage
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        binding = getDataBinding()
        viewModel = getActivityViewModelProvider(this).get(WineCellarViewModel::class.java)
        initView()
        initListener()
        initObserver()
        loadData()
    }

    private fun initListener() {
        binding.imgBack.setOnClickListener(this)
        binding.imgAdd.setOnClickListener(this)

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val bean = adapter.data[position] as WineryEvaluateListBean
            clickPosition = position
            when (view.id) {
                R.id.img_edit -> {
                    val bundle = Bundle()
                    bundle.putParcelable(Constants.KEY_COMMENT_BEAN,bean)
                    ARouterUtils.jump(ActivityRouter.WINE_CELLAR.PUBLISH_WINEECELLAR_COMMEENT, bundle)
                }
                R.id.img_del -> {
                    deleteOption(bean.id)
                }
            }
        }

        binding.smartRefresh.setOnRefreshListener {
            page = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
    }

    private fun initView() {
        binding.rvList.setAdapter(mAdapter)
    }

    private fun initObserver() {
        viewModel.evaluateListLD.observe(this, Observer {
            hideLoadingDialog()
            binding.smartRefresh.finishRefresh()
            if (it == null) {
                mAdapter.emptyView = EmptyViewUtil.getEmptyView4Winecellar(binding.rvList, "发布酒窖种草评论，把酒窖最好的一面分享给大家\n添加成功后，在“线上酒窖”进行预览", R.drawable.ic_empty_demo_comment)
                return@Observer
            }
            if (it.data?.size == 0 && page == 1) {//首次为空
                mAdapter.emptyView = EmptyViewUtil.getEmptyView4Winecellar(binding.rvList, "发布酒窖种草评论，把酒窖最好的一面分享给大家\n添加成功后，在“线上酒窖”进行预览", R.drawable.ic_empty_demo_comment)
            } else {
                if (page == 1) {
                    mAdapter.setNewData(it.data)
                } else {
                    mAdapter.addData(it.data!!)
                    binding.smartRefresh.finishLoadMore()
                }
                if (it.data.checkNotNull()) {
                    hasnext = it.totalCount != mAdapter.data.size
                }
                page++
            }
        })
        viewModel.deleteCommentLD.observe(this, Observer {
            if (it.checkNotNull() && it) {
                //删除评论
                mAdapter.remove(clickPosition)
                if (mAdapter.data?.size == 0) {//首次为空
                    mAdapter.emptyView = EmptyViewUtil.getEmptyView4Winecellar(binding.rvList, "发布酒窖种草评论，把酒窖最好的一面分享给大家\n添加成功后，在“线上酒窖”进行预览", R.drawable.ic_empty_demo_comment)
                }
                mAdapter.notifyDataSetChanged()
                HKToastUtils.showToast("删除成功")
            }
        })
    }

    fun loadData() {
        showLoadingDialog()
        val wineryCode = SpUtils.getInstance().getCurrentWineryInfo().wineryCode
        viewModel.requestEvaluateList(wineryCode,page)
    }

    private fun deleteOption(id: Long) {
        PopUtils.showOkCancelDialog(this, "确认删除", object : PopUtils.ICallBack {
            override fun clickOk() {
                viewModel.reqDeleteComment(id)
            }

            override fun clickCancel() {

            }

        }, false, "确认", "取消")
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.img_back -> {
                finish()
            }
            R.id.img_add -> {
                //发布评论
                if (!DoubleClickHelper.isOnDoubleClick()) {
                    ARouterUtils.jump(ActivityRouter.WINE_CELLAR.PUBLISH_WINEECELLAR_COMMEENT)
                }
            }
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType == MessageBus.msgId_publish_winery_comment_success) {
            page=1
            loadData()
        }
    }
    override fun onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroy()
    }
}